package edu.nwpu.service.impl;

import com.sun.istack.internal.NotNull;
import edu.nwpu.dao.UserMapper;
import edu.nwpu.pojo.User;
import edu.nwpu.service.UserService;
import edu.nwpu.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name = "userMapper")
    private UserMapper userMapper;

    @Override
    public Map<String, Object> login(String name, String password) {
        Map<String, Object> response = new HashMap<>();
        if (name == null || "".equals(name)){
            response.put("info", "用户名为空");
        } else if (password == null || "".equals(password)){
            response.put("info", "用户密码为空");
        } else {
            User u = userMapper.selectUserByNameAndPassword(name, password);
            if (u == null){
                response.put("info", "用户名或密码错误");
            } else {
                response.put("status", "success");
                response.put("token", TokenUtil.getToken(name, password));
                response.put("uid", u.getId());
                return response;
            }
        }
        response.put("status", "failed");
        return response;
    }

    @Override
    public Map<String, Object> register(String name, String password, String phoneNo) {
        Map<String, Object> response = new HashMap<>();
        if (name == null || "".equals(name)){
            response.put("info", "用户名为空");
        } else if (password == null || "".equals(password)) {
            response.put("info", "用户密码为空");
        } else {
            User u = userMapper.selectUserByName(name);
            if (u != null){
                response.put("info", "用户名已存在");
            } else {
                User newUser = new User();
                newUser.setName(name);
                newUser.setPassword(password);
                newUser.setPhoneNo(phoneNo);
                int ret = userMapper.insertUser(newUser);
                if (ret == 1) {
                    response.put("status", "success");
                    response.put("token", TokenUtil.getToken(name, password));
                    response.put("uid", newUser.getId());
                    return response;
                } else {
                    response.put("info", "注册失败");
                }
            }
        }
        response.put("status", "failed");
        return response;
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User getUserByNameAndPassword(@NotNull String name, @NotNull String password) {
        return userMapper.selectUserByNameAndPassword(name,password);
    }

    @Override
    public User getUserByIdAndPassword(Long id, String password) {
        return userMapper.selectUserByIdAndPassword(id, password);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public Map<String, Object> updateUserInfo(User user) {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "failed");
        if (null == user || null == user.getId()) {
            response.put("info", "缺少信息");
            return response;
        }
        User u = getUserById(user.getId());
        if (null == user.getName() || "".equals(user.getName())) {
            response.put("info", "新用户名不能为空");
            return response;
        }
        if (!u.getName().equals(user.getName()) && userMapper.selectUserByName(user.getName()) != null) {
            response.put("info", "新用户名与其他用户冲突");
            return response;
        }
        u.setName(user.getName());
        if (null != user.getPhoneNo() && !"".equals(user.getPhoneNo())) {
            u.setPhoneNo(user.getPhoneNo());
        }
        if (userMapper.updateUser(u) != 1) {
            response.put("info", "修改失败");
            return response;
        }
        response.put("status", "success");
        return response;
    }

    @Override
    public Map<String, Object> updateUserPassword(@NotNull Long id,
                                                  @NotNull String oldPwd,
                                                  @NotNull String newPwd,
                                                  @NotNull String newPwdRe) {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "failed");
        if (!newPwd.equals(newPwdRe)) {
            response.put("info", "密码不一致");
            return response;
        }
        if ("".equals(newPwd)) {
            response.put("info", "新密码不能为空");
            return response;
        }
        if (getUserByIdAndPassword(id, oldPwd) == null) {
            response.put("info", "密码错误");
            return response;
        }
        if (userMapper.updatePasswordByUserId(id, newPwd) != 1) {
            response.put("info", "修改失败");
        }
        response.put("status", "success");
        return response;
    }
}
